/**
*   FuzzyEngine - an Simple flex framework 
*   Copyright (C) 2010 Constantin Aldea
*
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package fuzzy.penelope.core.dataservices.rest {
	import fuzzy.penelope.model.Context;

	import project.vo.TestModuleVo;

	import com.googlecode.flexxb.FlexXBEngine;

	import mx.rpc.AsyncToken;

	[ExcludeClass]
	public class ServiceImpl implements IServiceFuzzy
	{
		public static var MODULE_IDENTIFIER:String = "MODULE_NAME";
		private var httpservicecall:ServiceFuzzy;
		
		public function testModuleCall(nomeServizio:String, id:String=null, input:Object=null,  listner:Function=null, setCursor:Boolean=true):AsyncToken
		{
			httpservicecall=impostahttpservicecall();
			var inputValue:Object;
			inputValue=input;
			if(id != null) return httpservicecall.callFuzzyServices(TestModuleVo.MODULE_NAME+"/"+id, nomeServizio, input,   listner, setCursor);
			else return httpservicecall.callFuzzyServices(TestModuleVo.MODULE_NAME, nomeServizio, input, listner, setCursor);
	
		}
		
		
		
		public function localRestCall(nomeFile : String, input : Object=null, listner : Function=null):AsyncToken
		{
			httpservicecall=impostahttpservicecall();
			var inputValue:Object;
			inputValue=input;
			return httpservicecall.callFuzzyLocalServices(nomeFile, listner);
		}


		
		public function restCall(module:Class, nomeServizio:String, id:String=null, input:Object=null,  listner:Function=null, setCursor:Boolean=true):AsyncToken
		{
			httpservicecall=impostahttpservicecall();
			var inputValue:Object;
			inputValue=input;
			if(!module is Class){
				throw new Error("il primo parametro deve essere una classe ");
			}
			
			if(!module.hasOwnProperty(MODULE_IDENTIFIER)){
				throw new Error("Il modulo non e corretamente configurato per la chiamata prototipale, aggiungere proprieta MODULE_NAME");
			}
		
			if(id != null) return httpservicecall.callFuzzyServices(module[MODULE_IDENTIFIER]+"/"+id, nomeServizio, input, listner, setCursor);
			else return httpservicecall.callFuzzyServices( module[MODULE_IDENTIFIER], nomeServizio, input, listner, setCursor);
	
		}
		


		private function impostahttpservicecall():ServiceFuzzy
		{
			var call:ServiceFuzzy=new ServiceFuzzy();
			call.useProxy=false;
			call.method="POST";
			call.contentType="application/xml";
			call.resultFormat="e4x";
			call.host=Context.getInstance().url_Fuzzy;
			return call;
		}
		
		public function traceObjectToXML(object : Object) : XML {
			trace("[DEBUG traceObjectToXML] this is the output of serializing an object" );
			trace(FlexXBEngine.instance.serialize(object));
			return FlexXBEngine.instance.serialize(object);
		}
		
		public function traceXMLToObject(object : XML) : * {
			trace("[DEBUG traceObjectToXML] this is the output of deserializing an object" );
			trace(FlexXBEngine.instance.deserialize(object));
			return FlexXBEngine.instance.deserialize(object);	
		}
	}
}